﻿using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Tp.Dto.EventArgs;
using Tp.Dto.Stammdaten;
using Tp.Interfaces.Commands.Stammdaten;
using Tp.Interfaces.Services;
using Tp.Interfaces.ViewModels.Stammdaten;
using System.Collections.Generic;


namespace Tp.StammdatenViewModel
{
	  public class PersonenUserControlViewModel : ViewModelBase, IPersonenUserControlViewModel
	  {

			#region Members
            
            private bool isInProcess;
            private List<PersonDto> personenList;
            private bool processIsStarting;
			private PersonDto selectedPerson;
			private IPersonenService service;
            private string titel = "Personen";
            
			#endregion

			#region Properties

            public IPersonenViewModelCommands Commands { get; set; }

            public bool IsInProcess
            {
                get { return this.isInProcess; }
                set
                {
                    if (this.isInProcess == value)
                    {
                        return;
                    }
                    this.isInProcess = value;
                    RaisePropertyChanged(() => IsInProcess);
                }
            }

			public string MyTitel
			{
				  get { return this.titel; }
				  set { this.titel = value; }
			}

            public List<PersonDto> PersonenList
            {
                get { return this.personenList; }
                set
                {
                    if (this.personenList != null && this.personenList == value)
                    {
                        return;
                    }

                    this.personenList = value;
                    RaisePropertyChanged(() => PersonenList);
                }
            }

            public bool ProcessIsStarting
            {
                get { return this.processIsStarting; }
                set
                {
                    if (this.processIsStarting == value)
                    {
                        return;
                    }
                    this.processIsStarting = value;
                    RaisePropertyChanged(() => ProcessIsStarting);
                }
            }

			public PersonDto SelectedPerson
			{
				  get { return this.selectedPerson; }
				  set
				  {
						if (this.selectedPerson == value)
						{
							  return;
						}

						if (this.selectedPerson != null)
						{
							  if (this.selectedPerson.IsDirty)
							  {
									MessageBoxResult res = MessageBox.Show("Daten wurden noch nicht gespeichert. Möchten Sie die Daten jetzt speichern?", "Person - " + this.selectedPerson.Vorname + " " + this.selectedPerson.Nachname, MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation);
									switch (res)
									{
										  case MessageBoxResult.Yes:
												this.service.SavePerson(SelectedPerson);
												break;
										  case MessageBoxResult.No:
												this.SelectedPerson.UndoChanges();
												break;
										  case MessageBoxResult.Cancel:
												return;

									}
							  }
						}

						this.selectedPerson = value;
						this.ProcessIsStarting = true;
						this.IsInProcess = false;
						if (this.SelectedPerson != null)
						{
							  this.SelectedPerson.IsDirty = false;
						}
						RaisePropertyChanged(() => SelectedPerson);
				  }
			}

			#endregion

			#region C-Tor

            public PersonenUserControlViewModel(IPersonenViewModelCommands commands, IPersonenService service)
			{
				  this.Commands = commands;
				  this.service = service;
				  this.RegisterMessengers();
			}


			#endregion

			#region Messengers
			
			private void RegisterMessengers()
			{
				  Messenger.Default.Register<PersonenOperationsEventArgs>(this, ExecuteMessage);
			}

			#endregion

			#region Methods 

			private void ExecuteMessage(PersonenOperationsEventArgs ea)
			{
                this.PersonenList = this.service.LoadPersons();
			}

            private void LoadPersons()
            {
                this.service.LoadPersons();
            }

			#endregion
	  }
}
